home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ldb.zip / BINDER.HPP < prev    next >
C/C++ Source or Header  |  1991-10-18  |  8KB  |  269 lines

  1. /*
  2.  
  3.     binder.hpp
  4.     10-18-91
  5.     Loose Data Binder v 1.4
  6.  
  7.     Copyright 1991
  8.     John W. Small
  9.     All rights reserved
  10.  
  11.     PSW / Power SoftWare
  12.     P.O. Box 10072
  13.     McLean, Virginia 22102 8072 USA
  14.  
  15.     John Small
  16.     Voice: (703) 759-3838
  17.     CIS: 73757,2233
  18.  
  19. */
  20.  
  21.  
  22. #ifndef BINDER_HPP
  23. #define BINDER_HPP
  24.  
  25. #include <limits.h>    // UINT_MAX
  26.  
  27. /*
  28.  
  29.     The "Loose Data Binder", or Binder for short, binds
  30.     any type of data together in a hybrid stack-queue-
  31.     deque-list-array structure.  Like a loose leaf
  32.     notebook where you can insert or arrange leafs in
  33.     any fashion, the "Loose Data Binder" allows you to
  34.     collect and arrange data in any fashion.  Think of
  35.     the Binder as an elastic array of void pointers.
  36.     The Binder is the equivalent of an extensive 
  37.     container class hierarchy but without the confusing 
  38.     complexity of a towering hierarchy.
  39.  
  40. */
  41.  
  42.  
  43. /*  Various pointers and their NULLs  */
  44.  
  45. typedef void * voiD;
  46. #define voiD0  ((voiD)0)
  47. typedef voiD * voiDV;
  48. #define voiDV0 ((voiDV)0)
  49.  
  50.  
  51. enum BDR_DEFAULTS  {
  52.  
  53.     BDR_MAXNODES    =    (UINT_MAX/sizeof(voiD)),
  54.     BDR_LIMIT    =    20,
  55.     BDR_DELTA    =    10,
  56.     BDR_NOTFOUND    =    BDR_MAXNODES
  57. };
  58.  
  59. enum BDR_FLAG_BITS  {
  60.  
  61.     BDR_SORTED    =    0x01,
  62.     BDR_OK_FREE    =    0x02,
  63.     BDR_NO_FREE    =    0x00
  64. };
  65.  
  66.  
  67. /*  Search/sort compare function pointer type:  */
  68.  
  69. typedef int (*BDRcomparE)(const voiD D1, const voiD D2);
  70. #define BDRcomparE0 ((BDRcomparE)0)
  71.  
  72.  
  73. /*  Function pointer types for the Binder iterators:  */
  74.  
  75. typedef void (*BDRforEachBlocK)
  76.         (voiD D, voiD M, voiD A);
  77. typedef int (*BDRdetectBlocK)
  78.         (voiD D, voiD M);
  79. class Binder;
  80. typedef Binder * BindeR;
  81. typedef void (*BDRcollectBlocK)
  82.         (voiD D, BindeR R, voiD M, voiD A);
  83.  
  84. /*
  85.     Gimmick for constructing without initializing: 
  86.     to be used for loading instances from a stream 
  87.     (see sbinder.hpp).
  88. */
  89.  
  90. typedef BindeR * UniqueBinderConST;
  91. #define OnlyInitBinderVFT ((UniqueBinderConST)0)
  92.  
  93.  
  94. /*  
  95.     Gaurrantees Dfree() is only invoked when 
  96.     (flags & BDR_OK_FREE) != 0.
  97. */
  98.  
  99. #define DFREE(Dexp) ((flags & BDR_OK_FREE)? Dfree(Dexp): 0)
  100.  
  101.  
  102. class Binder {
  103. protected:
  104.     unsigned lowLimit, lowThreshold, first;
  105.     voiDV linkS;
  106.     unsigned limit, delta, nodes, maxNodes;
  107.     unsigned curNode, flags;
  108.     BDRcomparE comparE;
  109.     void construct(unsigned flags, unsigned maxNodes,
  110.         unsigned limit, unsigned delta);
  111.     virtual int Dfree(voiD D) 
  112.         /*
  113.             Dfree() is called by any Binder 
  114.             primitive that attempts to delete
  115.             the data, D, it is unbinding, e.g. 
  116.             atFree(), popFree(), etc.  The 
  117.             Binder must be constructed with
  118.             BDR_OK_FREE for Dfree() to be
  119.             called.  In other words it is 
  120.             guaranteed that Dfree() is never
  121.             called when BDR_OK_FREE is absent.
  122.             If D is NULL then zero must be 
  123.             returned otherwise a nonzero value 
  124.             must be returned regardless of 
  125.             whether or not an overriding 
  126.             function is successful in deleting
  127.             its data.  Override this function to
  128.             implement calling your own node 
  129.             destructors.
  130.         */
  131.         { return (D? delete D, 1 : 0); }
  132.     virtual int Dattach(voiD)
  133.         /*
  134.             Dattach() is called by any Binder
  135.             primitive that attempts to bind 
  136.             data, e.g. atIns(), push(), etc.
  137.             This allows the data to become aware
  138.             of the binding process by overriding
  139.             Dattach().  The voiD parameter is a 
  140.             pointer to the data about to be 
  141.             bound and guaranteed never to be 
  142.             NULL!  If and only if the data can't
  143.             attach itself to the Binder, as 
  144.             defined by the overriding function,
  145.             should zero be returned.  Any 
  146.             overriding function should only
  147.             "link" itself to the Binder but 
  148.             should not use the implicit "this"
  149.             pointer to access Binder within the
  150.             overriding function.  This 
  151.             restriction applies because Binder
  152.             data may be in a transition state
  153.             when Dattach() is called.  It is 
  154.             however permissible to store the 
  155.             "this" pointer within the data being
  156.             bound for later use in accessing 
  157.             Binder data.
  158.         */
  159.         { return 1; }
  160.     virtual void Ddetach(voiD)
  161.         /*
  162.             Ddetach() is called by any Binder
  163.             primitive that attempts to unbind
  164.             data, e.g. atDel(), pop(), etc.
  165.             The voiD parameter is never NULL!
  166.             Once called, the data must consider
  167.             itself detached from the Binder!  
  168.         */
  169.         { return; }
  170. public:
  171.     Binder(UniqueBinderConST) {}
  172.     Binder(unsigned flags = BDR_NO_FREE,
  173.         unsigned maxNodes = BDR_MAXNODES,
  174.         unsigned limit = BDR_LIMIT,
  175.         unsigned delta = BDR_DELTA)
  176.         { construct(flags,maxNodes,limit,delta); }
  177.     Binder(voiDV argv, int argc = 0,
  178.         unsigned flags = BDR_NO_FREE);
  179.     voiDV vector();
  180.     virtual ~Binder();
  181.     unsigned Limit()  { return limit; }
  182.     unsigned setLimit(unsigned newLimit);
  183.     unsigned pack()  { return setLimit(nodes); }
  184.     unsigned Delta()  { return delta; }
  185.     unsigned setDelta(unsigned newDelta = BDR_DELTA);
  186.     unsigned Nodes()  { return nodes; }
  187.     unsigned MaxNodes()  { return maxNodes; }
  188.     unsigned setMaxNodes(unsigned newMaxNodes
  189.         = BDR_MAXNODES);
  190.     unsigned vacancy()  { return maxNodes - nodes; }
  191.     unsigned vacancyNonElastic()
  192.             { return limit - nodes; }
  193.     voiD atIns(unsigned n, const voiD D);
  194.     voiD atDel(unsigned n);
  195.     int  allDel();
  196.     int  atFree(unsigned n)  { return DFREE(atDel(n)); }
  197.     int  allFree();
  198.     voiD atPut(unsigned n, const voiD D);
  199.     voiD atGet(unsigned n);
  200.     voiD operator[](unsigned n)  { return atGet(n); }
  201.     voiD atXchg(unsigned n, const voiD D);
  202.     unsigned index(const voiD D);
  203.     voiD add(const voiD D)  { return atIns(nodes,D); }
  204.     Binder& operator+=(const voiD D)
  205.         { atIns(nodes,D); return *this; }
  206.     voiD subtract(const voiD D)
  207.         { return atDel(index(D)); }
  208.     Binder& operator-=(const voiD D)
  209.         { atDel(index(D)); return *this; }
  210.     int  forEach(BDRforEachBlocK B, voiD M = voiD0,
  211.         voiD A = voiD0);
  212.     unsigned firstThat(BDRdetectBlocK B, voiD M = voiD0);
  213.     unsigned lastThat(BDRdetectBlocK B, voiD M = voiD0);
  214.     int  collect(BDRcollectBlocK B, BindeR R,
  215.         voiD M = voiD0, voiD A = voiD0);
  216.  
  217. /*  FlexList like primitives:  */
  218.  
  219.     voiD top()  { return atGet(0); }
  220.     voiD current()  { return atGet(curNode); }
  221.     operator voiD()  { return atGet(curNode); }
  222.     /*
  223.         The implicit (voiD) cast is meant to be
  224.         used in conjunction with the iterators and
  225.         not the << and >> operators!
  226.     */
  227.     voiD bottom()  { return atGet(nodes-1); }
  228.     unsigned CurNode();
  229.     int  setCurNode(unsigned n = BDR_MAXNODES);
  230.     int  Sorted()  { return (flags & BDR_SORTED); }
  231.     void unSort()  { flags &= ~BDR_SORTED; }
  232.     BDRcomparE ComparE()  { return comparE; }
  233.     void setComparE(BDRcomparE comparE)
  234.         { this->comparE = comparE;
  235.             flags &= ~BDR_SORTED; }
  236.     voiD push(const voiD D)  { return atIns(0,D); }
  237.     voiD pop()  { return atDel(0); }
  238.     int  popFree()  { return DFREE(atDel(0)); }
  239.     Binder& operator>>(voiD& D)
  240.         { D = atDel(0); return *this; }
  241.     voiD insQ(const voiD D)
  242.         { return atIns(nodes,D); }
  243.     Binder& operator<<(const voiD D)
  244.         { atIns(nodes,D); return *this; }
  245.     Binder& operator<<(Binder& (*manipulator)
  246.         (Binder& B));
  247.     voiD rmQ()  { return atDel(0); }
  248.     int  rmQFree()  { return DFREE(atDel(0)); }
  249.     voiD unQ() /* Remove from rear of Q */
  250.         { return atDel(nodes-1); }
  251.     int  unQFree()  { return DFREE(atDel(nodes-1)); }
  252.     voiD ins(const voiD D);
  253.     voiD insSort(const voiD D);
  254.     voiD del();
  255.     int  delFree()  { return DFREE(del()); }
  256.     voiD next();
  257.     voiD operator++()  { return next(); }
  258.     voiD prev();
  259.     voiD operator--()  { return prev(); }
  260.     voiD findFirst(const voiD K);
  261.     voiD findNext(const voiD K);
  262.     voiD findLast(const voiD K);
  263.     voiD findPrev(const voiD K);
  264.     int  sort(BDRcomparE comparE = BDRcomparE0);
  265. };
  266.  
  267.  
  268. #endif
  269.